import { ref, computed } from "vue";
import { defineStore } from "pinia";



export const useDeviceStore = defineStore("device", () => {
  
  const deviceList = ref<any[]>([])
  const categoryOption = ref([
    {
      label: "种/繁设备",
      value: 1,
      children: [
        { label: "农机", value: 1 },
        { label: "农具", value: 2 },
        { label: "组培设备", value: 3 },
        { label: "其他", value: 0},
      ]
    },
    {
      label: "加工设备",
      value: 2,
      children: [
        { label: "拣选设备", value: 4 },
        { label: "清洗设备", value: 5 },
        { label: "剪切设备", value: 6 },
        { label: "干燥设备", value: 7 },
        { label: "保鲜设备", value: 8 },
        { label: "其他", value: 0 },
      ]
    
    },
    {
      label: "包装设备",
      value: 3,
      children: [
        { label: "充填设备", value: 9 },
        { label: "打包设备", value: 10 },
        { label: "封合设备", value: 11 },
        { label: "其它", value: 0 },
      ]
      
    },
    {
      label: "检测设备",
      value: 4,
      children: [
        { label: "检验仪器", value: 12 },
        { label: "检测工具", value: 13 },
        { label: "其它", value: 0 },
      ]
    },
    {
      label: "仓储物流设备",
      value: 5,
      children: [
        { label: "测量设备", value: 14 },
        { label: "装卸设备", value: 15 },
        { label: "养护设备", value: 16 },
        { label: "运输设备", value: 17 },
        { label: "其它", value: 0 },
      ]
      
    },
    {
      label: "货点设备",
      value: 6,
      children: [
        { label: "电子秤", value: 18 },
      ]
      
    },
  ])

  const getCategoryName = (value: number) => {
    for (let item of categoryOption.value) {
      if (item.value == value) {
        return item.label
      }
    }
  }

  const typeOption = (category?: number) => {
    if(!category){
      return []
    }
    for (let item of categoryOption.value) {
      if (item.value == category) {
        return item.children
      }
    }
    return []
  }

  const getTypeName = (category: number, value: number) => {
      let typeList = typeOption(category)
      for (let item of typeList) {
        if (item.value == value) {
          return item.label
        }
      }
  }


  const statusOption = ref([
    { label: "在用", value: 1 },
    { label: "停用", value: 0 },
  ]);

  return {
    deviceList,
    categoryOption,
    typeOption,
    statusOption,
    getCategoryName,
    getTypeName,

  };
}, {
  persist: true
});
